<?php

class RegistrarController extends Zend_Controller_Action
{
    public function init()
    {
        $formLogin = new Application_Form_Login();
        $this->view->formLogin = $formLogin;
        $model =new Application_Model_Noticias();
        $this-> view->noticias=$model->getAll();
        $modelTesis =new Application_Model_Tesis();
        $this-> view->tesis=$modelTesis->getAll();
        $modelPersona =new Application_Model_Personas();
        $this-> view->persona=$modelPersona->getAll();
        if(Zend_Auth::getInstance()->hasIdentity()){
            $skin=Zend_Auth::getInstance()->getIdentity()->skin;
            $this->_helper->Cambiarskin($skin);
        }
        $this->view->baseUrl = $this->_request->getBaseUrl();  
 
    }
    public function registroAction(){     
        
        $modelAreas=new Application_Model_Areas();
        $modelCarreras=new Application_Model_Carreras();
        $formRtesis=new Application_Form_Rtesis();
        $formAutor=new Application_Form_Autor();
        $formAutor3=new Application_Form_Autor3();
        $formAutor2=new Application_Form_Autor2();
        $formTutor=new Application_Form_Tutor();
        $formTutor2=new Application_Form_Tutor2();
        $formTutor3=new Application_Form_Tutor3();
        
        $tAreas=$modelAreas->getAll();
        $areas=array();
        $subAreas=array();
        $numElementos=count($tAreas);
        $numAux=0;
        if(count($tAreas)) {
            while($numAux<$numElementos){
                if($tAreas[$numAux]->nivel==1){
                    $areas[]=array($tAreas[$numAux]->nombre_area => $tAreas[$numAux]->nombre_area);
                }
                if($tAreas[$numAux]->nivel==2){
                    $subAreas[]=array($tAreas[$numAux]->nombre_area => $tAreas[$numAux]->nombre_area);
                }
                $numAux++;
            }
        }else{ 
            echo "no hay areas";
        } 
        $formRtesis->area->addMultiOptions($areas);
        $formRtesis->subArea->addMultiOptions($subAreas);
        
        $tCarreras=$modelCarreras->getAll();
        $carreras=array();
        $numCarreras=count($tCarreras);
        $numAux2=0;
        if(count($tCarreras)) {
            while($numAux2<$numCarreras){
                $carreras[]=array($tCarreras[$numAux2]->nombre_carrera => $tCarreras[$numAux2]->nombre_carrera);
                $numAux2++;
            }
        }else{ 
            echo "no hay carreras";
        } 
        $formRtesis->carrera->addMultiOptions($carreras);
        
        $this->view->Rtesis=$formRtesis;
        $this->view->Autor=$formAutor;
        $this->view->Autor2=$formAutor2;
        $this->view->Autor3=$formAutor3;
        $this->view->Tutor=$formTutor;
        $this->view->Tutor2=$formTutor2;
        $this->view->Tutor3=$formTutor3;
    }   
    public function pruebaAction(){
    	
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(); 
        $modelPersona2=new Application_Model_Persona2();
        $modelTutor=new Application_Model_Tutores();
        $modelAutor=new Application_Model_Autores();
        $modelRegistro=new Application_Model_Registros();
        $data = $this->_request->getPost();
        Zend_Loader::loadClass('Zend_Pdf');
        $pdf = Zend_Pdf::load(APPLICATION_PATH .'/forms/registros/prueba.pdf');
        $page = $pdf->pages[0];
        $font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
        $xi=40;
        $yi=810;
        $xf=560;
        $yf=680;
        $x=50;
        $x2=320;
        $y=790;
       
        if ($this->getRequest()->isXmlHttpRequest()) { //Detectamos si es una llamada AJAX 
            
            $modelRegistro->save($data['titulo'],$data['ciudad'],$data['pais'],$data['anio'],
                                 $data['facultad'],$data['area'],$data['subArea'],$data['modalidad'],
                                 $data['carrera']);
            $registro=$modelRegistro->getAll();
            $count3=count($registro);
            $id_registro=$registro[$count3-1]->id_registro;
            
            $modelPersona2->save2($data['nombreA'],$data['aPaternoA'],$data['aMaternoA'],$data['emailA']);
            $persona=$modelPersona2->getAll();
            $count=count($persona);
            $id_persona=$persona[$count-1]->id_persona;
            $modelAutor->save($id_persona,$id_registro);  
            
            $modelPersona2->save2($data['nombreT'],$data['aPaternoT'],$data['aMaternoT'],$data['emailT']);
            $persona2=$modelPersona2->getAll();
            $count2=count($persona2);
            $id_persona2=$persona2[$count2-1]->id_persona;
            $modelTutor->save($data['ci'],$data['profecion'],$id_persona2,$id_registro);
            
           
            
            $page->drawRectangle($xi, $yi, $xf, $yf,
            		$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);
            $page->setFont($font, 15);
            $page->drawText('Datos de Tesis', $x, $y);$y=$y-20;
            $page->setFont($font, 12);
            $page->drawText('Titulo del Proyecto:'."      ".$data['titulo'], $x, $y);$y=$y-15;
            $page->drawText('Facultad o Institucion:'."  ".$data['facultad'], $x, $y);$y=$y-15;
            $page->drawText('Carrera:'."  ".$data['carrera'], $x, $y);
            $page->drawText('Modalidad:'."  ".$data['modalidad'], $x2, $y);$y=$y-15;
            $page->drawText('Area:'."       ".$data['area'], $x, $y);
            $page->drawText('SubArea:'."    ".$data['subArea'], $x2, $y);$y=$y-15;
            $page->drawText('Ciudad:'."   ".$data['ciudad'], $x, $y);
            $page->drawText('Pais:'."           ".$data['pais'], $x2, $y);$y=$y-15;
            $page->drawText('Ano:'."        ".$data['anio'], $x, $y);
            $page->drawText('Codigo:'."       ".'dsdsdsds', $x2, 695);$y=$y-20;
            675;
            $page->drawRectangle($xi, $y, $xf, $y-70,
            		$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
            $page->setFont($font, 15);
            $page->drawText('Datos de Autor', $x, $y);$y=$y-20;
            $page->setFont($font, 12);
            $page->drawText('Nombre:'."       ".$data['nombreA'], $x, $y);
            $page->drawText('A. Paterno:'."  ".$data['aPaternoA'], $x2, $y);$y=$y-15;
            $page->drawText('A. Materno:'."  ".$data['aMaternoA'], $x, $y);
            $page->drawText('Email:'."          ".$data['emailA'], $x2, $y);
           
            
        }  
        if($data['dosAutores']=='true'){
        	$modelPersona2->save2($data['nombreA2'],$data['aPaternoA2'],$data['aMaternoA2'],$data['emailA2']);
        	$persona=$modelPersona2->getAll();
        	$count=count($persona);
        	$id_persona=$persona[$count-1]->id_persona;
        	$modelAutor->save($id_persona,$id_registro);
        	$y=$y-20;
        	$page->drawRectangle($xi, $y, $xf, $y-70,
        			$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
        	$page->setFont($font, 15);
        	$page->drawText('Datos de Autor 2', $x, $y);$y=$y-20;
        	$page->setFont($font, 12);
        	$page->drawText('Nombre:'."       ".$data['nombreA2'], $x, $y);
        	$page->drawText('A. Paterno:'."  ".$data['aPaternoA2'], $x2, $y);$y=$y-15;
        	$page->drawText('A. Materno:'."  ".$data['aMaternoA2'], $x, $y);
        	$page->drawText('Email:'."          ".$data['emailA2'], $x2, $y);
        }
        if($data['tresAutores']=='true'){
        	$modelPersona2->save2($data['nombreA3'],$data['aPaternoA3'],$data['aMaternoA3'],$data['emailA3']);
        	$persona=$modelPersona2->getAll();
        	$count=count($persona);
        	$id_persona=$persona[$count-1]->id_persona;
        	$modelAutor->save($id_persona,$id_registro);
        	$y=$y-20;
        	$page->drawRectangle($xi, $y, $xf, $y-70,
        			$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
        	$page->setFont($font, 15);
        	$page->drawText('Datos de Autor 3', $x, $y);$y=$y-20;
        	$page->setFont($font, 12);
        	$page->drawText('Nombre:'."       ".$data['nombreA3'], $x, $y);
        	$page->drawText('A. Paterno:'."  ".$data['aPaternoA3'], $x2, $y);$y=$y-15;
        	$page->drawText('A. Materno:'."  ".$data['aMaternoA3'], $x, $y);
        	$page->drawText('Email:'."          ".$data['emailA3'], $x2, $y);
        }
        $y=$y-20;
        $page->drawRectangle($xi, $y, $xf, $y-80,
        		$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
        $page->setFont($font, 15);
        $page->drawText('Datos de Tutor', $x, $y);$y=$y-20;
        $page->setFont($font, 12);
        $page->drawText('Nombre:'."       ".$data['nombreT'], $x, $y);
        $page->drawText('A. Paterno:'."  ".$data['aPaternoT'], $x2, $y);$y=$y-15;
        $page->drawText('A. Materno:'."  ".$data['aMaternoT'], $x, $y);
        $page->drawText('Email:'."          ".$data['emailT'], $x2, $y);$y=$y-15;
        $page->drawText('CI:'."                ".$data['ci'], $x, $y);
        $page->drawText('Profecion:'."    ".$data['profecion'], $x2, $y);;
        if($data['dosTutores']=='true'){
        	$modelPersona2->save2($data['nombreT2'],$data['aPaternoT2'],$data['aMaternoT2'],$data['emailT2']);
        	$persona2=$modelPersona2->getAll();
        	$count2=count($persona2);
        	$id_persona2=$persona2[$count2-1]->id_persona;
        	$modelTutor->save($data['ci2'],$data['profecion2'],$id_persona2,$id_registro);
        	$y=$y-20;
        	$page->drawRectangle($xi, $y, $xf, $y-80,
        			$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
        	$page->setFont($font, 15);
        	$page->drawText('Datos de Tutor 2', $x, $y);$y=$y-20;
        	$page->setFont($font, 12);
        	$page->drawText('Nombre:'."       ".$data['nombreT2'], $x, $y);
        	$page->drawText('A. Paterno:'."  ".$data['aPaternoT2'], $x2, $y);$y=$y-15;
        	$page->drawText('A. Materno:'."  ".$data['aMaternoT2'], $x, $y);
        	$page->drawText('Email:'."          ".$data['emailT2'], $x2, $y);$y=$y-15;
        	$page->drawText('CI:'."                ".$data['ci2'], $x, $y);
        	$page->drawText('Profecion:'."    ".$data['profecion2'], $x2, $y);530;
        }
        if($data['tresTutores']=='true'){
        	$modelPersona2->save2($data['nombreT3'],$data['aPaternoT3'],$data['aMaternoT3'],$data['emailT3']);
        	$persona2=$modelPersona2->getAll();
        	$count2=count($persona2);
        	$id_persona2=$persona2[$count2-1]->id_persona;
        	$modelTutor->save($data['ci3'],$data['profecion3'],$id_persona2,$id_registro);
        	$y=$y-20;
        	$page->drawRectangle($xi, $y, $xf, $y-80,
        			$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);$y=$y-20;
        	$page->setFont($font, 15);
        	$page->drawText('Datos de Tutor 3', $x, $y);$y=$y-20;
        	$page->setFont($font, 12);
        	$page->drawText('Nombre:'."       ".$data['nombreT3'], $x, $y);
        	$page->drawText('A. Paterno:'."  ".$data['aPaternoT3'], $x2, $y);$y=$y-15;
        	$page->drawText('A. Materno:'."  ".$data['aMaternoT3'], $x, $y);
        	$page->drawText('Email:'."          ".$data['emailT3'], $x2, $y);$y=$y-15;
        	$page->drawText('CI:'."                ".$data['ci3'], $x, $y);
        	$page->drawText('Profecion:'."    ".$data['profecion3'], $x2, $y);530;
        }
        $pdf->save(APPLICATION_PATH .'/forms/registros/registro_tesis_'.$id_registro.'.pdf');
    }
    
    public function listarareasAction()
    {
        $modelAreas =new Application_Model_Areas();
        $this-> view->registrar=$modelAreas->getAll();
      
    }
    public function agregarareaAction()
    {
     
        $formArea=new Application_Form_Area();
        
        if($this->getRequest()->isPost()){
            if($formArea->isValid($this->_getAllParams())){
                $modelAreas=new Application_Model_Areas();
                $modelAreas->save($formArea->getValues());
                return $this->_redirect('/registrar/listarareas');
            }
        }
        $this->view->formArea=$formArea;
       
    }
    public function editarareaAction(){
       
        if(!$this->hasParam('id_area')){
            return $this->_redirect('/registrar/listarareas');
        }
        $formArea=new Application_Form_Area();
        $modelArea2=new Application_Model_Areas();
        
        if($this->getRequest()->isPost()){
            if($formArea->isValid($this->_getAllParams())){
                $modelArea=new Application_Model_Areas();
                $modelArea->save($formArea->getValues(),$this->_getParam('id_area'));
                return $this->_redirect('/registrar/listarareas');
            }
        }else{
            $row=$modelArea2->getRow($this->_getParam('id_area'));
            if($row){
                $formArea->populate($row->toArray());
                
            }else{
               return $this->_redirect('/registrar/listarareas'); 
            }
        }
        $this->view->formArea=$formArea;
    }
    public function borrarareaAction(){
     
        if(!$this->hasParam('id_area')){
            return $this->_redirect('/registrar/listarareas');
        }
      
        $modelArea2=new Application_Model_Areas();
        $row=$modelArea2->getRow($this->getParam('id_area'));
        if($row){
            $row->delete();
            return $this->_redirect('/registrar/listarareas');
        }
        return $this->_redirect('/registrar/listarareas');
    }
    public function listarcarrerasAction()
    {
        $modelCarreras =new Application_Model_Carreras();
        $this-> view->registrar=$modelCarreras->getAll();
      
    }
    public function agregarcarreraAction()
    {
     
        $formCarrera=new Application_Form_Carrera();
        
        if($this->getRequest()->isPost()){
            if($formCarrera->isValid($this->_getAllParams())){
                $modelCarrera=new Application_Model_Carreras();
                $modelCarrera->save($formCarrera->getValues());
                return $this->_redirect('/registrar/listarcarreras');
            }
        }
        $this->view->formCarrera=$formCarrera;
       
    }
    public function editarcarreraAction(){
       
        if(!$this->hasParam('id_carrera')){
            return $this->_redirect('/registrar/listarcarrera');
        }
        $formCarrera=new Application_Form_Carrera();
        $modelCarrera2=new Application_Model_Carreras();
        
        if($this->getRequest()->isPost()){
            if($formCarrera->isValid($this->_getAllParams())){
                $modelCarrera=new Application_Model_Carreras();
                $modelCarrera->save($formCarrera->getValues(),$this->_getParam('id_carrera'));
                return $this->_redirect('/registrar/listarcarreras');
            }
        }else{
            $row=$modelCarrera2->getRow($this->_getParam('id_carrera'));
            if($row){
                $formCarrera->populate($row->toArray());
                
            }else{
               return $this->_redirect('/registrar/listarcarreras'); 
            }
        }
        $this->view->formCarrera=$formCarrera;
    }
    public function borrarcarreraAction(){
     
        if(!$this->hasParam('id_carrera')){
            return $this->_redirect('/registrar/listarcarreras');
        }
      
        $modelCarrera2=new Application_Model_Carreras();
        $row=$modelCarrera2->getRow($this->getParam('id_carrera'));
        if($row){
            $row->delete();
            return $this->_redirect('/registrar/listarcarreras');
        }
        return $this->_redirect('/registrar/listarcarreras');
    }
    public function crearpdf($nombreA,$aPaternoA,$aMaternoA,$emailA,$nombreT,$aPaternoT,$aMaternoT,$emailT,$ci,$profecion){
    	Zend_Loader::loadClass('Zend_Pdf');
    	$pdf = Zend_Pdf::load(APPLICATION_PATH .'/forms/prueba.pdf');
    	$page = $pdf->pages[0];
    	$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
    	$page->setFont($font, 12);
  
    	$page->drawRectangle(40, 810, 560, 680,
    	$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    	$page->setFont($font, 15);
    	$page->drawText('Datos de Tesis', 50, 790);
    	$page->setFont($font, 12);
    	$page->drawText('Titulo del Proyecto:'."      ".$nombreA, 50, 770);
    	$page->drawText('Facultad o Institucion:'."  ".$aPaternoA, 50, 755);
    	$page->drawText('Carrera:'."  ".$aMaternoA, 50, 740);
    	$page->drawText('Modalidad:'."  ".$emailA, 320, 740);
    	$page->drawText('Area:'."       ".$ci, 50, 725);
    	$page->drawText('SubArea:'."    ".$profecion, 320, 725);
    	$page->drawText('Ciudad:'."   ".$ci, 50, 710);
    	$page->drawText('Pais:'."           ".$profecion, 320, 710);
    	$page->drawText('Ano:'."        ".$ci, 50, 695);
    	$page->drawText('Codigo:'."       ".$profecion, 320, 695);
    	
    	
    	$page->drawRectangle(40, 675, 560, 605,
    	$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    	$page->setFont($font, 15);
    	$page->drawText('Datos de Autor', 50, 655);
    	$page->setFont($font, 12);
    	$page->drawText('Nombre:'."       ".$nombreA, 50, 635);
    	$page->drawText('A. Paterno:'."  ".$aPaternoA, 320, 635);
    	$page->drawText('A. Materno:'."  ".$aMaternoA, 50, 620);
    	$page->drawText('Email:'."          ".$emailA, 320, 620);
    	
    	
    	
    	$page->drawRectangle(40, 600, 560, 520,
    	$fillType = Zend_Pdf_Page::SHAPE_DRAW_STROKE);
    	$page->setFont($font, 15);
    	$page->drawText('Datos de Tutor', 50, 580);
    	$page->setFont($font, 12);
    	$page->drawText('Nombre:'."       ".$nombreA, 50, 560);
    	$page->drawText('A. Paterno:'."  ".$aPaternoA, 320, 560);
    	$page->drawText('A. Materno:'."  ".$aMaternoA, 50, 545);
    	$page->drawText('Email:'."          ".$emailA, 320, 545);
    	$page->drawText('CI:'."                ".$ci, 50, 530);
    	$page->drawText('Profecion:'."    ".$profecion, 320, 530);
    	
    	
    	$pdf->save(APPLICATION_PATH .'/forms/prueba1.pdf');
    }
}